<?php
// +----------------------------------------------------------------------
// | 产品修改附属操作，例如：美团推送
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/09/18
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/area.inc.php';

// write_logs(json_encode($_REQUEST));

// 发布及保存
if($_REQUEST['action']=='CreateNewTicket'){
	
	// 有效期变更
	if(isset($_REQUEST['validHtml_1']) && $_REQUEST['validHtml_1']!=$_REQUEST['validHtml_2']){
		
		$cache_file = '/var/www/html/new/d/logs/ProdEffective_u.php';
		$cache_arry = file_exists($cache_file) ? include $cache_file:array();
		$prods_ids = $_REQUEST['pid'];
		$sql = "select l.title as ltitle,t.title as ttitle,p.id as pid,l.area from uu_products p inner join uu_jq_ticket t on p.id=t.pid inner join uu_land l on t.landid=l.id where p.id=$prods_ids limit 1";
		$GLOBALS['le']->query($sql);
		$p_info = $GLOBALS['le']->fetch_assoc();

		$province = ''; 
		$city     = '';
		if($p_info['area']){
			$area_a = explode('|', $p_info['area']);
			if(isset($areas[$area_a[0]])) $province = $areas[$area_a[0]];
			if(isset($areas[$area_a[1]])) $city     = $areas[$area_a[1]];
		}
		
		$cache_arry = array_merge_custom($cache_arry, array($p_info['pid'] => array(
				'time' 	   => date('Y-m-d H:i:s'), 
				'ltitle'   => $p_info['ltitle'], 
				'ttitle'   => $p_info['ttitle'],
				'province' => $province,
				'city'     => $city,
				'validHtml_1' => $_REQUEST['validHtml_1'],
				'validHtml_2' => $_REQUEST['validHtml_2'],
		)));
		
		if(count($cache_arry)>100){
			$uKey = array_shift($cache_arry);
			unset($cache_arry[$uKey]);
		}
		
		$cache_arry = "<?php\nreturn ".var_export($cache_arry, true).';';
		@file_put_contents($cache_file, $cache_arry);
	}
	
	// 最新发布产品也要有上架通知
	if($_REQUEST['add_ticket']==1){
		$pStatus = 1;
		$prods_ids  = $_REQUEST['pid'];
		goto SetProdStatus;
	}
}




// 景区上下架
if($_REQUEST['action']=='SetSceneryState'){
	$lid = $_REQUEST['lid'];
	if($_REQUEST['state']==2){// 景区下架
		$sql = "select t.pid from uu_jq_ticket t left join uu_products p on t.pid=p.id where t.landid=$lid";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $prods_ids[] = $row['pid'];
		$pStatus = 2;
		$prods_ids  = implode(',', $prods_ids);
		goto SetProdStatus;
	}
}

// 产品上下架
if($_REQUEST['action']=='SetProdStatus'){
	
	$prods_ids = $_REQUEST['ids'];
	$pStatus   = $_REQUEST['status'];
	
	SetProdStatus:
	$cache_file = '/var/www/html/new/d/logs/ProdStatus_'.$pStatus.'.php';
	$cache_arry = file_exists($cache_file) ? include $cache_file:array();
	$status_box = array(1=>'上架', 2=>'下架');

	$sql = "select l.title as ltitle,t.title as ttitle,p.id as pid,l.area from uu_products p inner join uu_jq_ticket t on p.id=t.pid inner join uu_land l on t.landid=l.id where p.id in ($prods_ids)";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		
		// 省份
		$province = ''; $city = '';
		if($row['area']){
			$area_a   = explode('|', $row['area']);
			if(isset($areas[$area_a[0]])) $province = $areas[$area_a[0]];
			if(isset($areas[$area_a[1]])) $city     = $areas[$area_a[1]];
		}
		
		$cache_arry = array_merge_custom($cache_arry, array(
			$row['pid'] => array('time' => date('Y-m-d H:i:s'), 'ltitle' => $row['ltitle'], 'ttitle' => $row['ttitle'], 'province' => $province, 'city' => $city ))
		);
		
		
		if(count($cache_arry)>100){
			$uKey = array_shift($cache_arry);
			unset($cache_arry[$uKey]);
		}
	}
	
	$cache_arry = "<?php\nreturn ".var_export($cache_arry, true).';';
	@file_put_contents($cache_file, $cache_arry);
}




// 美团、去哪儿产品更改推送
include '/var/www/html/new/d/module/api/api_product_changenotice/Product_change_notification.php';


function array_merge_custom($a1, $a2){
	foreach($a2 as $key=>$value) $a1[$key] = $value;
	return $a1;
}

function write_logs($txt,$file='/var/www/html/new/d/abc/logs/1.txt'){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}